/*
 * File Name：Square.java
 * Copyright：Copyright 2008-2012 CiWong.Inc. All Rights Reserved.
 * Description： Square.java
 * Modify By：PLA-ZJLIU
 * Modify Date：2012-12-28
 * Modify Type：Add
 */
package com.ciwong.ciwongwrite.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.ciwong.ciwongwrite.R;


/**
 * <pre>
 * </pre>
 *
 * @author PLA-ZJLIU
 * @version ciwong v.1.0 2012-12-28
 * @since ciwong v.1.0
 */
public class Square extends View
{
    private static final int COLOR_RED = 0;

    private static final int COLOR_BLACK = 1;

    private static final int COLOR_BLUE = 2;

    private boolean isSelected;

    private int colorEnum;

    private int color;

    private Bitmap drawBitmap;

    private Resources mResources;

    public Square(Context context)
    {
        super(context);
        init();
    }

    public Square(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.graph);
        colorEnum = a.getColor(R.styleable.graph_colors, COLOR_BLACK);
        isSelected = a.getBoolean(R.styleable.graph_checked, false);
        a.recycle();
        init();
    }

    public Square(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.graph);
        colorEnum = a.getColor(R.styleable.graph_colors, Color.BLACK);
        a.recycle();
        init();
    }

    /**
     * 获取颜色
     *
     * @return
     */
    public int getColor()
    {
        return color;
    }

    private void init()
    {
        mResources = getContext().getResources();
        getDrawBitmap();
        setLayoutParams(new LayoutParams(drawBitmap.getWidth(),
                drawBitmap.getHeight()));
    }

    /**
     * 设置颜色
     *
     * @return 颜色值
     */
    public int setSelected()
    {
        isSelected = true;
        invalidate();
        return color;
    }

    public void cancelSelected()
    {
        isSelected = false;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        getDrawBitmap();
        canvas.drawBitmap(drawBitmap, 0, 0, mPaint);
    }

    private void getDrawBitmap()
    {
        int resId = R.mipmap.paint_style_balck;
        if(colorEnum==COLOR_BLACK){
            color = Color.BLACK;
            resId = isSelected ? R.mipmap.paint_style_balck_select
                    : R.mipmap.paint_style_balck;
        }else if(colorEnum==COLOR_BLUE){
            color = Color.BLUE;
            resId = isSelected ? R.mipmap.paint_style_blue_select
                    : R.mipmap.paint_style_blue;
        }
        else if(colorEnum==COLOR_RED){
            color = Color.RED;
            resId = isSelected ? R.mipmap.paint_style_red_select
                    : R.mipmap.paint_style_red;
        }
        drawBitmap = BitmapFactory.decodeResource(mResources, resId);
    }
}
